/***************************************************************************//**
 * @file GUI/PaneMain.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef PANE_MAIN_CPP
#define PANE_MAIN_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <cmath>

#include <QFile>
#include <QSettings>

#include <mscsim_defs.h>

#include <GUI/PaneMain.h>
#include <ui_PaneMain.h>

////////////////////////////////////////////////////////////////////////////////

PaneMain::PaneMain( QWidget *parent ) :
    QDockWidget ( parent ),
    m_ui ( new Ui::PaneMain )
{
    m_ui->setupUi( this );

    QFile file( "data/aircrafts/aircraft_types.xml" );

    m_ui->comboAircraftTypes->read( &file );

    settingsRead();
}

////////////////////////////////////////////////////////////////////////////////

PaneMain::~PaneMain()
{
    settingsWrite();

    if ( m_ui ) delete m_ui; m_ui = 0;
}

////////////////////////////////////////////////////////////////////////////////

void PaneMain::on_comboInitHeading_currentIndexChanged( int index )
{
    if ( index == 0 )
    {
        m_ui->spinInitHeading->setDecimals( 2 );   // rad
    }
    else
    {
        m_ui->spinInitHeading->setDecimals( 0 );   // deg
    }
}

////////////////////////////////////////////////////////////////////////////////

void PaneMain::on_pushInitDialog_clicked()
{
    emit pushInitDialogClicked();
}

////////////////////////////////////////////////////////////////////////////////

void PaneMain::settingsRead()
{
    QSettings settings( MSCSIM_ORG_NAME, MSCSIM_APP_NAME );
    
    settings.beginGroup( "pane_main" );
    {
        settings.beginGroup( "units_combos" );
        {
            m_ui->comboInitAirspeed ->setCurrentIndex( settings.value( "init_airspeed" , 0 ).toInt() );
            m_ui->comboInitAltitude ->setCurrentIndex( settings.value( "init_altitude" , 0 ).toInt() );
            m_ui->comboInitHeading  ->setCurrentIndex( settings.value( "init_heading"  , 1 ).toInt() );
        }
        settings.endGroup();
    }
    settings.endGroup();
}

////////////////////////////////////////////////////////////////////////////////

void PaneMain::settingsWrite()
{
    QSettings settings( MSCSIM_ORG_NAME, MSCSIM_APP_NAME );
    
    settings.beginGroup( "pane_main" );
    {
        settings.beginGroup( "units_combos" );
        {
            settings.setValue( "init_airspeed" , m_ui->comboInitAirspeed ->currentIndex() );
            settings.setValue( "init_altitude" , m_ui->comboInitAltitude ->currentIndex() );
            settings.setValue( "init_heading"  , m_ui->comboInitHeading  ->currentIndex() );
        }
        settings.endGroup();
    }
    settings.endGroup();
}
